package lanqiao;

import java.util.Scanner;
import java.util.jar.JarOutputStream;

public class 数组变换 {
    public static void main(String[] args) {
        int[] arr = new int[]{3,7,5,1,9};
        Scanner scanner = new Scanner(System.in);
        int d= scanner.nextInt();
        int m = arr[0]%d;
        for (int i = 1; i < arr.length ; i++) {
            if (arr[i]%d!=m){
                System.out.println(-1);
                break;
            }
        }
        int[][] arr2 = new int[arr.length][arr.length];
        for (int i = 0; i < arr.length ; i++) {
             arr2[i][0] = arr[i];
             arr2[i][1] = arr[i]/d;
        }
        int min= Integer.MAX_VALUE;
        for (int i = 0; i < arr.length; i++) {
            int cur = 0;
            for (int j = 0; j < arr.length; j++) {
                cur+=Math.abs(arr2[i][1]-arr2[j][1]);
            }
            min = cur<=min?cur:min;
        }
        System.out.println(min);
    }
}
